package com.gxljc.bear.crawler.util;

import com.gxljc.commons.util.StringUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Properties;

@Service("mailService")
@Scope("prototype")
public class MailService {
	Logger logger=Logger.getLogger(this.getClass());
	private String mailTo = "bear@summba.com";
	private String mailFrom = "operator-g@summba.com";
	private String mailHost = "smtp.exmail.qq.com";
	private int mailPort = 465;
	private String mailUser = "operator-g@summba.com";
	private String mailPassword = "minMa741";
	private boolean mailSmtpAuth = true;
	private int mailSmtpTimeout = 25000;
	private String mailSmtpSocketFactoryClass = "javax.net.ssl.SSLSocketFactory";
	private boolean mailSmtpSocketFactoryFallback = false;

	public static final long ONE_HOUR_IN_MILLISECONDS = 3600000l;
	private JavaMailSenderImpl mailSender;
	private SimpleMailMessage mailMessage;



	public void sendMail(String subject, String text) throws MessagingException{
		if(StringUtils.isEmpty(text)){
			logger.info("email filer");
			return;
		}
		mailMessage = new SimpleMailMessage();
		mailMessage.setFrom(getMailFrom());
		
		mailSender = new JavaMailSenderImpl();
		mailSender.setHost(getMailHost());
		mailSender.setPort(getMailPort());
		mailSender.setUsername(getMailUser());
		mailSender.setPassword(getMailPassword());
		Properties javaMailProperties = new Properties();
		javaMailProperties.put("mail.smtp.auth", isMailSmtpAuth());
		javaMailProperties.put("mail.smtp.timeout", getMailSmtpTimeout());
		javaMailProperties.put("mail.smtp.starttls.enable", true);
		javaMailProperties.put("mail.smtp.socketFactory.class", mailSmtpSocketFactoryClass);
		javaMailProperties.put("mail.smtp.socketFactory.fallback", mailSmtpSocketFactoryFallback);
		
		mailSender.setJavaMailProperties(javaMailProperties);
		// send to many, each must be separated by ","
		mailMessage.setTo(mailTo);
		MimeMessage msg = mailSender.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(msg, true, "utf-8");
		helper.setTo(mailMessage.getTo());
		helper.setFrom(mailMessage.getFrom());
		helper.setSubject(subject);
		helper.setText(text, true);
		mailSender.send(msg);
	}

	public static String getHostName(){
		String hostName = "";
		try {
			InetAddress IP = InetAddress.getLocalHost();
			hostName = IP.getHostName();
		} catch (UnknownHostException e1) {
			e1.printStackTrace();
		}
		return hostName;
	}

	public JavaMailSenderImpl getMailSender() {
		return mailSender;
	}

	public void setMailSender(JavaMailSenderImpl mailSender) {
		this.mailSender = mailSender;
	}

	public SimpleMailMessage getMailMessage() {
		return mailMessage;
	}

	public void setMailMessage(SimpleMailMessage mailMessage) {
		this.mailMessage = mailMessage;
	}

	public String getMailTo() {
		return mailTo;
	}

	public void setMailTo(String mailToUsers) {
		this.mailTo = mailToUsers;
	}

	public String getMailFrom() {
		return mailFrom;
	}

	public void setMailFrom(String mailFromUser) {
		this.mailFrom = mailFromUser;
	}

	public String getMailHost() {
		return mailHost;
	}

	public void setMailHost(String mailHost) {
		this.mailHost = mailHost;
	}

	public int getMailPort() {
		return mailPort;
	}

	public void setMailPort(int mailPort) {
		this.mailPort = mailPort;
	}

	public String getMailUser() {
		return mailUser;
	}

	public void setMailUser(String mailUser) {
		this.mailUser = mailUser;
	}

	public String getMailPassword() {
		return mailPassword;
	}

	public void setMailPassword(String mailPassword) {
		this.mailPassword = mailPassword;
	}

	public boolean isMailSmtpAuth() {
		return mailSmtpAuth;
	}

	public void setMailSmtpAuth(boolean mailSmtpAuth) {
		this.mailSmtpAuth = mailSmtpAuth;
	}

	public int getMailSmtpTimeout() {
		return mailSmtpTimeout;
	}

	public void setMailSmtpTimeout(int mailSmtpTimeout) {
		this.mailSmtpTimeout = mailSmtpTimeout;
	}

//	public boolean isMailSmtpStarttlsEnable() {
//		return mailSmtpStarttlsEnable;
//	}
//
//	public void setMailSmtpStarttlsEnable(boolean mailSmtpStarttlsEnable) {
//		this.mailSmtpStarttlsEnable = mailSmtpStarttlsEnable;
//	}
//
//	public String getMailSmtpSocketFactoryClass() {
//		return mailSmtpSocketFactoryClass;
//	}
//
//	public void setMailSmtpSocketFactoryClass(String mailSmtpSocketFactoryClass) {
//		this.mailSmtpSocketFactoryClass = mailSmtpSocketFactoryClass;
//	}
//
//	public boolean isMailSmtpSocketFactoryFallback() {
//		return mailSmtpSocketFactoryFallback;
//	}
//
//	public void setMailSmtpSocketFactoryFallback(
//			boolean mailSmtpSocketFactoryFallback) {
//		this.mailSmtpSocketFactoryFallback = mailSmtpSocketFactoryFallback;
//	}
//
//	public void setMailSubject(String mailSubject) {
//		this.servOffLineSubject = mailSubject;
//	}
	

}
